<script>
export default {
  data() {
    return {
      show: false,
      requestParam: {
        id: null,
        // Unit name
        name: null,
        fileIdOfIcon: null,
        description: null,
      }
    }
  },
  methods: {
    open(item) {
      if(item){
        this.requestParam = JSON.parse(JSON.stringify(item));
      }else{
        this.requestParam = {
          id: null,
          name: null,
          fileIdOfIcon: null,
          description: null,
        }
      }
      this.show = true;
    },
    close() {
      this.show = false;
      this.requestParam = {
        id: null,
        // Unit name
        name: null,
        fileIdOfIcon: null
      }
    },
    // 上传图片
    uploadImg() {
      this.$kt.file.chooseImageOne().then(res => {
        let loading = this.$loading({
          lock: true,
          text: '上传中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.2)'
        });
        this.$kt.request.uploadFile({
          file: res.file,
          groupCode: "iot",
          stateSuccess: (res) => {
            this.requestParam.fileIdOfIcon = res.data.id;
            loading.close();
          },
          stateFail: (res) => {
            loading.close();
            this.$message.error(res.errMsg);
          }
        });
      });
    },
    // /bus-iot-web/admin/sensorUnit
    toSave() {
      let loading = this.$loading({
        lock: true,
        text: '保存中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.$kt.request.send({
        uri: "/bus-iot-web/admin/sensorUnit/save",
        data: this.requestParam,
        stateSuccess: (res) => {
          this.$emit("save", res.data);
          loading.close();
          this.close();
        },
        stateFail: (res) => {
          loading.close();
          this.$message.error(res.errMsg);
        }
      });
    },
  }
}
</script>

<template>
  <el-dialog
      title="Save Unit"
      v-model="show"
      style="width: 300px">
    <div class="box">

      <el-form
          label-position="top"
          :model="requestParam" label-width="120px">
        <el-form-item label="Unit name">
          <el-input
              placeholder="Unit name"
              v-model="requestParam.name"></el-input>
        </el-form-item>
        <el-form-item label="Icon">
          <div class="upl">
            <img class="upl-img"
                 v-if="requestParam.fileIdOfIcon"
                 :src="$kt.file.visit(requestParam.fileIdOfIcon)"/>
            <div
                @click="uploadImg()"
                class="upl-btn"
            >Upload Icon
            </div>
          </div>
        </el-form-item>
        <el-form-item label="Description">
          <el-input
              type="textarea"
              placeholder="Description"
              v-model="requestParam.description"></el-input>
        </el-form-item>

      </el-form>
    </div>

    <div style="height: 50px"></div>

    <!-- 确认按钮 -->
    <div
        @click="toSave()"
        class="confirm-btn">
      Confirm
    </div>

  </el-dialog>
</template>

<style scoped lang="scss">

.upl {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: rgba(118, 118, 118, .1);
  border-radius: 5px;

  .upl-img {
    width: 100%;
    height: 100%;
  }

  .upl-btn {
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 0 5px 0 5px;
    background: #333333;
    color: #ffffff;
    cursor: pointer;
    font-size: 12px;
    box-sizing: border-box;
    border-radius: 0 0 5px 5px;
  }

}

.upl-btn:hover {
  background: #666666;
}

.upl-btn:active {
  background: #333333;
}

.box{
  position: relative;
  text-align: center;
}

.confirm-btn{
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: #333333;
  color: #ffffff;
  cursor: pointer;
  font-size: 14px;
}

.confirm-btn:hover{
  background: #666666;
}

.confirm-btn:active{
  background: #333333;
}

</style>